﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_TourDestinationAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            ValidateControl();
            BindDataToLanguageDropDown();
            BindDataToCountryDropdown();
            BindDataWhenEdit();
        }
    }
    
    #region Relative Functions
    private void ValidateControl()
    {
        int nCurrentLang = Support.GetCurrentLanguage();
        rfvTourDestinationname.ErrorMessage = MessagesValidate.DestinationTour_EMPTY[nCurrentLang -1];
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblCountry.Text = MultipleLanguage.LBL_COUNTRY[nDefaultLanguage - 1];
        lblRegion.Text = MultipleLanguage.REGION[nDefaultLanguage - 1];
        lblContent.Text = MultipleLanguage.LBL_CONTENT[nDefaultLanguage - 1];
        lblImage.Text = MultipleLanguage.LBL_IMAGE[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblTourDestinationName.Text = MultipleLanguage.TOURDESTINATION[nDefaultLanguage - 1];
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
    }
    private void BindDataWhenEdit()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    TourDestination objTourDestination = new TourDestination();
                    objTourDestination.LoadById(nID);

                    int nCountry = 0;
                    int nRegion = -1;
                    if (objTourDestination.Data.RegionID > 0)
                    {
                        Region objRegion = new Region();
                        objRegion.LoadById(objTourDestination.Data.RegionID);
                        ddlCountry.SelectedValue = ConvertData.ConvertToString(objRegion.Data.CountryID);
                        nCountry = objRegion.Data.CountryID;
                        nRegion = objTourDestination.Data.RegionID;
                    }
                    BindDataToRegionDropdown(nCountry);
                    ddlRegion.SelectedValue = ConvertData.ConvertToString(nRegion);

                    switch (nCurrentLanguage)
                    {
                        case 1:
                            txtTourDestinationName.Text = ConvertData.ConvertToString(objTourDestination.Data.TourDestinationTitle1);
                            FTBContent.IntialText(ConvertData.ConvertToString(objTourDestination.Data.TourDestinationDetail1));
                            break;
                        case 2:
                            txtTourDestinationName.Text = ConvertData.ConvertToString(objTourDestination.Data.TourDestinationTitle2);
                            FTBContent.IntialText(ConvertData.ConvertToString(objTourDestination.Data.TourDestinationDetail2));
                            break;
                        case 3:
                            txtTourDestinationName.Text = ConvertData.ConvertToString(objTourDestination.Data.TourDestinationTitle3);
                            FTBContent.IntialText(ConvertData.ConvertToString(objTourDestination.Data.TourDestinationDetail3));
                            break;
                    }

                    if (objTourDestination.Data.Image != "")
                        imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TOURDESTINATION_UPLOAD + objTourDestination.Data.Image;
                    else objTourDestination.Data.Image = Config.IMAGE_NO_IMAGE;
                }
            }
            else
            {
                BindDataToRegionDropdown(0);
                imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDESTINATION;
            Response.Redirect(sLink);
        }
    }
    private void BindDataToCountryDropdown()
    {
        DataTable dtbCountry = new DataTable();
        Country objCountry = new Country();
        dtbCountry = objCountry.GetALL();

        ddlCountry.DataSource = dtbCountry;
        ddlCountry.DataValueField = "CountryID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlCountry.DataTextField = "CountryName1";
                break;
            case 2:
                ddlCountry.DataTextField = "CountryName2";
                break;
            case 3:
                ddlCountry.DataTextField = "CountryName3";
                break;
        }
        ddlCountry.DataBind();
        Utl.SetNonSelectToControl(ddlCountry, MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllValueItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataToRegionDropdown(int nCountry)
    {
        DataTable dtbRegion = new DataTable();
        Region objRegion = new Region();
        dtbRegion = objRegion.SearchByCountry(nCountry);
        ddlRegion.DataSource = dtbRegion;
        ddlRegion.DataValueField = "RegionID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlRegion.DataTextField = "RegionName1";
                break;
            case 2:
                ddlRegion.DataTextField = "RegionName2";
                break;
            case 3:
                ddlRegion.DataTextField = "RegionName3";
                break;
        }
        ddlRegion.DataBind();
        Utl.SetNonSelectToControl(ddlRegion, MultipleLanguage.DDL_SELECTVALUE[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllValueItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private eTourDestination TourDestinationEntity()
    {
        eTourDestination entityTourDestination = new eTourDestination();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            TourDestination objTransport = new TourDestination();
            objTransport.LoadById(nItemID);
            entityTourDestination = objTransport.Data;
        }
        return entityTourDestination;
    }
    private eTourDestination SetData()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);

        TourDestination objTourDestination = new TourDestination();
        eTourDestination eTourDestination = this.TourDestinationEntity();
        
        int nRegionID = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        objTourDestination.Data.RegionID = nRegionID;
        if (iptImage.Value != "")
        {
            Support.DeleteFile("tourdestination", eTourDestination.Image);
            objTourDestination.Data.Image = Support.UploadFile(iptImage, "tourdestination");
        }
        if (iptImage.Value == "")
        {
            if (eTourDestination.Image != "")
                objTourDestination.Data.Image = ConvertData.ConvertToString(eTourDestination.Image);
            else objTourDestination.Data.Image = "";
        }

        switch (nCurrentLanguage)
        {
            case 1:
                objTourDestination.Data.TourDestinationTitle1 = txtTourDestinationName.Text.Trim();
                objTourDestination.Data.TourDestinationDetail1 = ConvertData.ConvertToString(FTBContent.RenderText());

                objTourDestination.Data.TourDestinationTitle2 = ConvertData.ConvertToString(eTourDestination.TourDestinationTitle2);
                objTourDestination.Data.TourDestinationDetail2 = ConvertData.ConvertToString(eTourDestination.TourDestinationDetail2);
                objTourDestination.Data.TourDestinationTitle3 = ConvertData.ConvertToString(eTourDestination.TourDestinationTitle3);
                objTourDestination.Data.TourDestinationDetail3 = ConvertData.ConvertToString(eTourDestination.TourDestinationDetail3);
                break;
            case 2:
                objTourDestination.Data.TourDestinationTitle2 = txtTourDestinationName.Text.Trim();
                objTourDestination.Data.TourDestinationDetail2 = ConvertData.ConvertToString(FTBContent.RenderText());

                objTourDestination.Data.TourDestinationTitle1 = ConvertData.ConvertToString(eTourDestination.TourDestinationTitle1);
                objTourDestination.Data.TourDestinationDetail1 = ConvertData.ConvertToString(eTourDestination.TourDestinationDetail1);
                objTourDestination.Data.TourDestinationTitle3 = ConvertData.ConvertToString(eTourDestination.TourDestinationTitle3);
                objTourDestination.Data.TourDestinationDetail3 = ConvertData.ConvertToString(eTourDestination.TourDestinationDetail3);
                break;
            case 3:
                objTourDestination.Data.TourDestinationTitle3 = txtTourDestinationName.Text.Trim();
                objTourDestination.Data.TourDestinationDetail3 = ConvertData.ConvertToString(FTBContent.RenderText());

                objTourDestination.Data.TourDestinationTitle2 = ConvertData.ConvertToString(eTourDestination.TourDestinationTitle2);
                objTourDestination.Data.TourDestinationDetail2 = ConvertData.ConvertToString(eTourDestination.TourDestinationDetail2);
                objTourDestination.Data.TourDestinationTitle1 = ConvertData.ConvertToString(eTourDestination.TourDestinationTitle1);
                objTourDestination.Data.TourDestinationDetail1 = ConvertData.ConvertToString(eTourDestination.TourDestinationDetail1);
                break;
        }

        return objTourDestination.Data;
    }
    #endregion

    #region Click Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        int nRegionID = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        if (nRegionID > 0)
        {
            TourDestination objTourDestination = new TourDestination();

            objTourDestination.Data = this.SetData();
            int nResult = 0;
            string sLink = "";
            string sAction = Request.QueryString[Config.ACTION];
            switch (sAction)
            {
                case Config.ACTION_ADD:
                    nResult = objTourDestination.Insert();
                    if (nResult > 0)
                    {
                        sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDESTINATION + "&" + Config.MESS_ID + "=2";
                        Response.Redirect(sLink);
                    }
                    else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                    break;
                case Config.ACTION_EDIT:
                    int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                    nResult = objTourDestination.Update(nID);
                    if (nResult > 0)
                    {

                        Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                        if (objTourDestination.Data.Image.ToString() != "")
                        {
                            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TOURDESTINATION_UPLOAD + objTourDestination.Data.Image.ToString();
                        }
                        if (objTourDestination.Data.Image.ToString() == "")
                        {
                            objTourDestination.Data.Image = Config.ROOT + "/Images/no_image.gif";
                        }

                    }
                    else
                        Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                    break;
            }
        }
        else
        {
            Alert1.ShowMessage(2, MessagesValidate.REGION_SELECT[nDefaultLanguage]);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDESTINATION);

    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCountry = ConvertData.ConvertToInt(ddlCountry.SelectedValue);
        BindDataToRegionDropdown(nCountry);
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        BindDataWhenEdit();
    }
    #endregion

    #region BindDataToDDL
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    #endregion
}